package cn.edu.henu.team.ui;

import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.edu.henu.team.R;
import cn.edu.henu.team.adapter.HotPostAdapter;
import cn.edu.henu.team.base.BaseActivity;
import cn.edu.henu.team.bean.hot_post;
import cn.edu.henu.team.bean.post;

public class HotPostingsActivity extends BaseActivity {
    private ImageView back;
    private List<hot_post> data = new ArrayList<>();//必须初始化
    private RecyclerView recyclerView;
    private HotPostAdapter adapter;

    @Override
    protected int initLayout() {
        return R.layout.activity_hot_postings;
    }

    @Override
    protected void initView() {
        back =  findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        recyclerView = findViewById(R.id.recyclerview);
    }

    @Override
    protected void initData() {
        //查询收藏数排前的笔记本
        BmobQuery<post> query = new BmobQuery("post");
        query.order("-like_sum");
        query.findObjects(new FindListener<post>() {
            @Override
            public void done(List<post> list, BmobException e) {
                if (e == null) {
                    if (list.size() != 0) {
                        for (post t : list) {
                            String img_url = "no_image";
                            if (t.getImage() != null)
                                img_url = t.getImage().getFileUrl();
                            hot_post flag = new hot_post(t.getObjectId().toString(), t.getContent().toString(), img_url);
                            data.add(flag);
                        }
                        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
                        recyclerView.setLayoutManager(layoutManager);
                        adapter = new HotPostAdapter(data);
                        recyclerView.setAdapter(adapter);
                    }
                } else {
                    Toast.makeText(getApplicationContext(), "查询失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}